package com.example.service;

import com.example.mapper.UserMapper;
import com.example.pojo.Order;
import com.example.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;



/**
 * @Description:
 * @author: GaoPeng
 * @date: 2021/5/26 21:40
 */
@RequestMapping("/userService")
@RestController
@Service
@CrossOrigin
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ObjectMapper jsonMapper;

    /**
     * 通过username（手机号）查询用户是否存在
     * @param username
     * @return
     */
    @GetMapping("/checkIfUserExistByUsername/{username}")
    Boolean checkIfUserExistByUsername(@PathVariable String username){
        return userMapper.selectCountUser(username)>0;
    }

    /**
     * 登录
     * @param username 手机号
     * @param password
     * @return
     */
    @PostMapping("/login")
    Boolean login(@RequestParam("username")String username,@RequestParam("password")String password){
        return userMapper.selectPasswordByUsername(username).equals(password);
    }

    /**
     *
     * @param username 手机号
     * @param password 密码
     * @param status 管理员或普通用户
     * @param realname 真实姓名
     * @param city 城市
     * @param address 地址
     * @param Post_code 邮编
     * @param Idcard_no 证件号码
     * @param idcard_kind 证件类型
     * @param email 电子邮箱
     * @return
     */
    @PostMapping("/register")
    Boolean register(@RequestParam("username")String username,@RequestParam("password")String password,@RequestParam("status")Integer status,@RequestParam("realname")String realname,@RequestParam("city")String city
            ,@RequestParam("address")String address,@RequestParam("Post_code")String Post_code,@RequestParam("Idcard_no")String Idcard_no,@RequestParam("Idcard_kind")String idcard_kind,@RequestParam("email")String email){
        if (userMapper.selectCountUser(username)>0) {
            return Boolean.FALSE;
        }
        User user=new User();
        user.setId(null);
        user.setUsername(username);
        user.setPassword(password);
        user.setStatus(status);
        user.setRealname(realname);
        user.setCity(city);
        user.setAddress(address);
        user.setPost_code(Post_code);
        user.setIdcard_no(Idcard_no);
        user.setIdcard_kind(idcard_kind);
        user.setEmail(email);
        return userMapper.insert(user)>0;
    }

    /**
     * 修改用户信息
     * @param username 手机号
     * @param password
     * @param status 普通用户，管理员
     * @param realname 真实姓名
     * @param city 所在城市
     * @param address 详细地址
     * @param Post_code 邮编
     * @param Idcard_no 证件号码
     * @param idcard_kind 证件类型
     * @param email 邮箱
     * @return
     */
    @PostMapping("/modifyUserInfo")
    Boolean modifyUserInfo(@RequestParam("username")String username,@RequestParam("password")String password,@RequestParam("status")Integer status,@RequestParam("realname")String realname,@RequestParam("city")String city
            ,@RequestParam("address")String address,@RequestParam("post_code")String post_code,@RequestParam("idcard_no")String idcard_no,@RequestParam("idcard_kind")String idcard_kind,@RequestParam("email")String email){
        if (userMapper.selectCountUser(username)==0) {
            return Boolean.FALSE;
        }
        Integer id = userMapper.selectIdByUsername(username);
        User user=new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setStatus(status);
        user.setRealname(realname);
        user.setCity(city);
        user.setAddress(address);
        user.setPost_code(post_code);
        user.setIdcard_no(idcard_no);
        user.setIdcard_kind(idcard_kind);
        user.setEmail(email);

        return userMapper.modifyUser(user)>0;
    }

    /**
     * 通过username获取user_id
     * @param username
     * @return
     */
    @GetMapping("/getIdByUsername/{username}")
    Integer getIdByUsername(@PathVariable String username){
       return userMapper.selectIdByUsername(username);
    }

    @GetMapping("/getAllUsers")
    String getAll(){
        try {
            return jsonMapper.writeValueAsString(userMapper.selectAll());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 插入一条公告
     * @param notice
     * @return
     */
    @PostMapping("/addNotice")
    Boolean addNotice(@RequestParam("notice")String notice){
        return userMapper.insertNotice(notice)>0;
    }

    /**
     * 获取所有公告
     */
    @GetMapping("/getAllNotice")
    String getAllNotice(){
        try {
            return jsonMapper.writeValueAsString(userMapper.selectAllNotice());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }
}
